{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# chap5 if语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.1 一个简单的示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Audi\n",
      "BMW\n",
      "Subaru\n",
      "Toyota\n"
     ]
    }
   ],
   "source": [
    "cars = ['audi', 'bmw', 'subaru', 'toyota']\n",
    "\n",
    "for car in cars:\n",
    "    if car == 'bmw':\n",
    "        print(car.upper())\n",
    "    else:\n",
    "        print(car.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查示例中的循环首先检查当前的汽车名是否是‘bmw’。如果是，就全部大写打印它，若不是则首字母大写打印它。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2 条件测试"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每条if语句的核心都是一个值为TRUE或者FALSE的表达式，这种表达式被称为条件测试。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意：** Python中检查是否相等时区分大小写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3 检查条件符号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用>, >=, <, <=, ==, !=来检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 < 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 == 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.4 多个条件检查"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and 和 or。and当所有条件都为真时，才为真。or至少一个条件为真就为真。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0 = 22\n",
    "age_1 = 18\n",
    "age_0 >= 21 and age_1 >= 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0 = 22\n",
    "age_1 = 18\n",
    "age_0 >= 21 or age_1 >= 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.5 if语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sorry, you are too young to vote.\n",
      "Please register to vote as soon as you turn 18!\n"
     ]
    }
   ],
   "source": [
    "age = 17\n",
    "if age >= 18:\n",
    "    print(\"You are only enough to vote！\")\n",
    "    print(\"Have you registered to vote yet?\")\n",
    "else:\n",
    "    print(\"Sorry, you are too young to vote.\")\n",
    "    print(\"Please register to vote as soon as you turn 18!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是常见的if语句格式。当然还有if - elif - else的多重判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your admission cost is $5.\n"
     ]
    }
   ],
   "source": [
    "age = 12\n",
    "if age < 4:\n",
    "    print(\"Your admission cost is $0.\") \n",
    "elif age < 18:\n",
    "    print(\"Your admission cost is $5.\")\n",
    "else:\n",
    "    print(\"Your admission cost is $10.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding mushrooms.\n",
      "Adding extra cheese.\n",
      "\n",
      " Finished making your pizza!\n"
     ]
    }
   ],
   "source": [
    "requested_toppings = ['mushrooms', 'extra cheese']\n",
    "\n",
    "if 'mushrooms' in requested_toppings:\n",
    "    print(\"Adding mushrooms.\")\n",
    "if 'peppenroni' in requested_toppings:\n",
    "    print(\"Adding pepperoni.\")\n",
    "if \"extra cheese\" in requested_toppings:\n",
    "    print(\"Adding extra cheese.\")\n",
    "print(\"\\n Finished making your pizza!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在本章中，你学习了如何编写结果要么为True，要么为False的条件测试。你学习了如何编写简单的if语句，if-else语句和if-elif-else结构。在程序中，你使用了这些结构来测试特定的条件，以确定这些条件是否满足。你学习了如何在利用高效的for循环的同时，以不同于其他元素的方式对特定元素进行处理。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
